package svc.elib.socnet;

import java.io.IOException;

import org.apache.commons.collections15.Transformer;


import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.io.PajekNetWriter;

import svc.elib.db.Author;
import svc.elib.db.Database;

public class Export {

	private class NodeTransformer implements Transformer<Author, String> {
		@Override
		public String transform(Author arg0) {
			return arg0.getName();
		}
	}
	
	private class LinkTransformer implements Transformer<Link, Number> {
		@Override
		public Number transform(Link arg0) {
			return arg0.getWeight();
		}
	}
	
	public void exportToPajek(UndirectedSparseGraph<Author, Link> graphToExport, String outFilePath) 
		throws IOException
	{
		PajekNetWriter<Author, Link> pajko = new PajekNetWriter<Author, Link>();
		pajko.save(graphToExport, outFilePath, new NodeTransformer(), new LinkTransformer());
	}
	
	public static void main(String[] args) 
		throws IOException
	{
		Database db = new Database("eLibData.csv", 1932, 2011);
		SocConstructor soc = new SocConstructor(db);
		Net net = soc.getNet();
	
		Export e = new Export();
		e.exportToPajek(net.getGraph(), "export/elib.net");
		
		ConnectedComponents ccf = new ConnectedComponents(net);
		ccf.resolveComponents();
		Net largest = ccf.getComponents().get(0);
		e.exportToPajek(largest.getGraph(), "export/elib_largest.net");
		Net secondLargest = ccf.getComponents().get(1);
		e.exportToPajek(secondLargest.getGraph(), "export/elib_2largest.net");
	}	
}
